package com.example.mall.web.controller;

import com.example.mall.domain.OrderAddresses;
import com.example.mall.dto.OrderAddressesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.OrderAddressesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IOrderAddressesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderAddresses")
public class OrderAddressesController {

    @Autowired
    public IOrderAddressesService orderAddressesService;

    /**
     * 分页查询订单地址表信息
     * @param orderAddressesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询订单地址表信息",notes = "分页查询订单地址表信息",httpMethod = "GET")
    public JSONResult queryPage(OrderAddressesQuery orderAddressesQuery){
        PageDTO<OrderAddressesDto> orderAddressesDtoPageDTO = orderAddressesService.queryPage(orderAddressesQuery);
        return JSONResult.success("查询成功",orderAddressesDtoPageDTO);
    }

    /**
     * 根据id查询订单地址表信息
     * @param addressId
     * @return
     */
    @GetMapping("query/{addressId}")
    @ApiOperation(value = "根据id查询订单地址表信息",notes = "根据id查询订单地址表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("addressId")Long addressId){
        //1.根据id查询用户信息
        if (addressId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ORDER_ADDRESSID_NOT_ID);
        }
        OrderAddresses orderAddresses = orderAddressesService.getById(addressId);
        return JSONResult.success("查询成功",orderAddresses);
    }

    /**
     * 根据id查询订单地址表信息
     * @param addressId
     * @return
     */
    @GetMapping("queryOrderAddressesById/{addressId}")
    @ApiOperation(value = "根据id查询订单地址表信息",notes = "根据id查询订单地址表信息",httpMethod = "GET")
    public JSONResult queryOrderAddressesById(@PathVariable("addressId")Long addressId){
        //1.根据id查询用户信息
        if (addressId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ORDER_ADDRESSID_NOT_ID);
        }
        OrderAddresses orderAddresses = orderAddressesService.queryOrderAddressesById(addressId);
        return JSONResult.success("查询成功",orderAddresses);
    }

    /**
     * 查询全部订单地址表信息
     * @param
     * @return
     */
    @GetMapping("queryOrderAddressesList")
    @ApiOperation(value = "查询全部订单地址表信息",notes = "查询全部订单地址表信息",httpMethod = "GET")
    public JSONResult queryOrderAddressesList(){
        List<OrderAddresses> orderAddresses = orderAddressesService.queryOrderAddressesList();
        return JSONResult.success("查询成功",orderAddresses);
    }

    /**
     * 新增订单地址表
     * @param orderAddressesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增订单地址表",notes = "新增订单地址表",httpMethod = "POST")
    public JSONResult insert(@RequestBody OrderAddressesDto orderAddressesDto){
        orderAddressesService.insert(orderAddressesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改订单地址表信息
     * @param orderAddressesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改订单地址表信息",notes = "修改订单地址表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody OrderAddressesDto orderAddressesDto){
        orderAddressesService.update(orderAddressesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除订单地址表信息
     * @param addressId
     * @return
     */
    @PostMapping("delete/{addressId}")
    @ApiOperation(value = "删除订单地址表信息",notes = "删除订单地址表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("addressId") Long addressId) {
        orderAddressesService.delete(addressId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除订单地址表信息
     * @param addressIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除订单地址表信息",notes = "批量删除订单地址表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> addressIds) {
        orderAddressesService.deleteBatch(addressIds);
        return JSONResult.success("批量删除成功");
    }
}
